<?php

namespace app\admin\controller;

use app\common\lib\IAuth;
use think\Controller;


class User extends Base
{
    public $model = 'User';

    public function index()
    {
        $user = model('User')->where('status', 1)->order('id', 'desc')->paginate(10);
        return $this->fetch('', [
            'user' => $user,
        ]);
    }

    public function add()
    {
        //判断是否post提交
        if (request()->isPost()) {
            $data = input('post.');
            $validate = validate('AdminUser');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $data['password'] = IAuth::setPassword($data['password']);
            $data['status'] = 1;

            try {
                $id = model('User')->add($data);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            if ($id) {
                $this->success('id=' . $id . '的用户新增成功');
            } else {
                $this->error('error');
            }

        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        if (request()->isPost()) {


        } else {
            try {
                $data = input('param.');
                $user = model('User')->get(['id' => $data['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $this->error($e->getMessage()));
            }
            return $this->fetch('', [
                'data' => $user,
            ]);
        }

    }


}
